<?php

//站内邮箱控制器
class Message extends Controller {

	var $viewpath;
	var $is_logined;
	var $prepage = 20;
	
	function Message()
	{
		parent::Controller();
		$this->viewpath = 'admin/'.__CLASS__;
		$this->load->model('managers_model','mm');
		$this->load->model("message_model");
		$this->is_logined = $this->mm->is_logined();
	}
	
	//默认首页
	function index()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();	
		//获取列表
		$offset = u(4);
		$items = $this->message_model->listitem(0, $this->prepage, $offset);
		//分页数据处理
		$this->load->library('pagination');
		$config['base_url'] = site_url($this->config->item('admin_path')."/message/index/");
		$config['total_rows'] = $this->message_model->countitems();
		$config['per_page'] = $this->prepage;
		$config['uri_segment'] = 4;
		$config['num_links'] = 1;
		$config['first_link'] = '首页';
		$config['last_link'] = '末页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$this->pagination->initialize($config);
		$pagestring = $this->pagination->create_links();
		
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$viewdata['items'] = $items;
		$viewdata['pagestring'] = $pagestring;
		$this->load->view($this->viewpath.'/index', $viewdata);
	}
	
	//查看
	function show()
	{
		$userid = u(4);
		$messageid = u(5);
		//获取邮件信息
		$row = $this->message_model->show($userid, $messageid);
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['row'] = $row;
		$this->load->view($this->viewpath.'/show', $viewdata);
	}
	
	//发信
	function post()
	{
		//处理表单提交
		if ($dosubmit = p("dosubmit")) {
			$senduser = p("select2");
			a($senduser);
		}
		
		//搜索字符处理
		parse_str($_SERVER["QUERY_STRING"], $this->param);
		$keyword = "";
		if (isset($this->param["keyword"])) {
			$keyword = safeEncoding(trim($this->param["keyword"]));//字符转换处理
		}
		//获取用户列表
		$this->load->model('accounts_model','am');
		$users = $this->am->listitem(0, 0, "id", "desc", $keyword);
		
		$viewdata["users"] = $users;
		$viewdata['keyword'] = $keyword;
		$this->load->view($this->viewpath.'/post', $viewdata);
	}
	
	//执行发信
	function dopost()
	{
		//获取数据
		$users = p("users");
		$title = p("title");
		$description = p("description");
		$users = explode(",", $users);
		foreach ($users as $userid) {
			if ($userid != "") {
				//系统发送信息
				$this->message_model->sys_send($userid, $title, $description);
			}
		}
//		admin_show_success("发送成功", $this->config->item('admin_path')."/message/index");
		$jumpurl = gurl($this->config->item('admin_path')."/message/index");
		echo $jumpurl;
		exit;
	}
	
	//单个删除
	function delete()
	{
		if(!$id = u(4)) redirect($this->config->item('admin_path')."/message/index");
		//删除
		$this->message_model->delete($id);
		admin_show_success("删除成功", $this->config->item('admin_path')."/message/index");	
	}
	
	//选择删除
	function delselected()
	{
		if(!$delstr = u(4)) redirect($this->config->item('admin_path')."/message/index");
		//删除
		$this->message_model->delselected($delstr);
		admin_show_success("删除成功", $this->config->item('admin_path')."/message/index");	
	}
}

/* End of file */